import numpy as np


def get_positional_encoding(max_position, d_model):
    # 初始化位置编码矩阵
    positional_encoding = np.zeros((max_position, d_model))

    # 计算位置编码
    for pos in range(max_position):
        for i in range(d_model):
            if i % 2 == 0:
                positional_encoding[pos, i] = np.sin(pos / (10000 ** (2 * i / d_model)))
            else:
                positional_encoding[pos, i] = np.cos(pos / (10000 ** ((2 * (i - 1)) / d_model)))

    return positional_encoding


# 示例参数
max_position = 50  # 最大位置
d_model = 512  # 模型维度

# 生成位置编码
positional_encoding = get_positional_encoding(max_position, d_model)

# 打印位置编码矩阵的形状
print("Positional Encoding Shape:", positional_encoding.shape)

# 打印前几个位置编码值
print("First few positional encodings:")
print(positional_encoding[:5, :10])